package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import com.joker.orange.domain.CaricatureInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.OrderBo;
import com.joker.orange.domain.vo.OrderVo;
import com.joker.orange.domain.Order;
import com.joker.orange.mapper.OrderMapper;
import com.joker.orange.service.IOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单管理Service业务层处理
 *
 * @author Joker
 * @date 2024-07-31
 */
@RequiredArgsConstructor
@Service
public class OrderServiceImpl implements IOrderService {

    private final OrderMapper baseMapper;

    /**
     * 查询订单管理
     */
    @Override
    public OrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单管理列表
     */
    @Override
    public TableDataInfo<OrderVo> queryPageList(OrderBo bo, PageQuery pageQuery) {
        QueryWrapper<Order> wrapper = Wrappers.query();
        wrapper.like(StringUtils.isNotBlank(bo.getOrderName()), "o.order_name", bo.getOrderName())
            .like(StringUtils.isNotBlank(bo.getOrderNo()), "o.order_no", bo.getOrderNo())
            .eq(StringUtils.isNotBlank(bo.getOrderStatus()), "o.order_status", bo.getOrderStatus())
            .eq(StringUtils.isNotBlank(bo.getOrderType()), "o.order_type", bo.getOrderType());
        Page<OrderVo> result = baseMapper.queryPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单管理列表
     */
    @Override
    public List<OrderVo> queryList(OrderBo bo) {
        LambdaQueryWrapper<Order> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Order> buildQueryWrapper(OrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Order> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getOrderName()), Order::getOrderName, bo.getOrderName());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), Order::getOrderNo, bo.getOrderNo());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderStatus()), Order::getOrderStatus, bo.getOrderStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderType()), Order::getOrderType, bo.getOrderType());
        return lqw;
    }

    /**
     * 新增订单管理
     */
    @Override
    public Boolean insertByBo(OrderBo bo) {
        Order add = BeanUtil.toBean(bo, Order.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单管理
     */
    @Override
    public Boolean updateByBo(OrderBo bo) {
        Order update = BeanUtil.toBean(bo, Order.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Order entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
